package com.linkoog.devtools.view_remark;

import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.ide.projectView.ProjectViewNodeDecorator;
import com.intellij.openapi.components.impl.stores.IProjectStore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.packageDependencies.ui.PackageDependenciesNode;
import com.intellij.project.ProjectKt;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import com.linkoog.devtools.utils.StringUtils;
import com.linkoog.devtools.view_remark.persistent.ViewRemarkConfigPersistent;
import com.linkoog.devtools.view_remark.persistent.ViewRemarkDataPersistent;

public class ViewRemarkProjectViewDecorator implements ProjectViewNodeDecorator {


    @Override
    public void decorate(ProjectViewNode<?> node, PresentationData data) {
        final Project project = node.getProject();
        final ViewRemarkConfigPersistent viewRemarkConfigPersistent = ViewRemarkConfigPersistent.getInstance(project);
        final ViewRemarkDataPersistent viewRemarkDataPersistent = ViewRemarkDataPersistent.getInstance(project);

        final VirtualFile virtualFile = node.getVirtualFile();
        if (virtualFile == null) return;

        IProjectStore store = ProjectKt.getStateStore(project);
        final VirtualFile projectDir = VfsUtil.findFile(store.getProjectBasePath(), false);
        if (projectDir == null) return;

        String relativePath = VfsUtilCore.getRelativePath(virtualFile, projectDir);

        //设置别名，设置别名后，不在显示前缀和后缀
        final String rawText = data.getPresentableText();
        if (viewRemarkConfigPersistent.isShowAlias()){
            String alias = getNodeAlias(viewRemarkDataPersistent, relativePath);
            if (StringUtils.isNotBlank(alias)){
                data.setPresentableText(alias);
                data.setTooltip(rawText);
                return;
            }
        }

        //添加前缀
        String text = rawText;
        data.setTooltip(rawText);
        if (viewRemarkConfigPersistent.isShowPrefix()){
            String prefix = getNodePrefix(viewRemarkDataPersistent, relativePath);
            if (StringUtils.isNotBlank(prefix)){
                prefix = "[" + prefix +"]";
                text = prefix + " " + rawText;
                data.setPresentableText(text);
            }
        }

        //添加后缀
        if (viewRemarkConfigPersistent.isShowSubfix()){
            String subfix = getNodeSubfix(viewRemarkDataPersistent, relativePath);
            if (StringUtils.isNotBlank(subfix)){
                String separator = SystemInfo.isWindows ? "  " : "\t";
                data.addText(text, SimpleTextAttributes.REGULAR_ATTRIBUTES);
                data.addText(separator + subfix, SimpleTextAttributes.GRAY_ATTRIBUTES);
            }
        }
    }


    private String getNodeAlias(ViewRemarkDataPersistent viewRemarkDataPersistent, String relativePath){
        return viewRemarkDataPersistent.getAlias(relativePath);
    }

    private String getNodePrefix(ViewRemarkDataPersistent viewRemarkDataPersistent, String relativePath){
        return viewRemarkDataPersistent.getPrefix(relativePath);
    }

    private String getNodeSubfix(ViewRemarkDataPersistent viewRemarkDataPersistent, String relativePath){
        return viewRemarkDataPersistent.getSubfix(relativePath);
    }

}
